package com.lipy.springootrabbitmq.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ChannelListener;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: lipy
 * @create: 2020-09-22 22:29
 **/
@Configuration
public class RabbitMQConfig {

    public static final String FANOUT_EXCHANGE = "FANOUT_EXCHANGE";
    public static final String TOPIC_EXCHANGE = "TOPIC_EXCHANGE";

    @Bean
    public RabbitTemplate RabbitTemplate(ConnectionFactory connectionFactory){
        CachingConnectionFactory cachingConnectionFactory = (CachingConnectionFactory)connectionFactory;
        cachingConnectionFactory.setPublisherReturns(true);

        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.out.println("RabbitMQConfig.confirm correlationData = [" + correlationData + "], b = [" + b + "], s = [" + s + "]");
            }
        });
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("RabbitMQConfig.ReturnCallback message = [" + message + "], i = [" + i + "], s = [" + s + "], s1 = [" + s1 + "], s2 = [" + s2 + "]");
            }
        });

        rabbitTemplate.setUsePublisherConnection(true);
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }

    @Bean
    public Queue fanouta(){
        return new Queue("fanout.aa");
    }

    @Bean
    public Queue fanoutb(){
        return new Queue("fanout.bb");
    }
    @Bean
    public Queue manage(){
        return new Queue("manage");
    }
    @Bean
    public Queue object(){
        return new Queue("object");
    }
    @Bean
    public Queue topica(){
        return new Queue("topic.A");
    }
    @Bean
    public Queue topicall(){
        return new Queue("topic.All");
    }

    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    @Bean
    public Binding bindingExchangeA(FanoutExchange fanoutExchange, Queue fanouta){
        return BindingBuilder.bind(fanouta).to(fanoutExchange);
    }

    @Bean
    public Binding bindingExchangeB(FanoutExchange fanoutExchange, Queue fanoutb){
        return BindingBuilder.bind(fanoutb).to(fanoutExchange);
    }

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    @Bean
    public Binding bindingTopicA(TopicExchange topicExchange, Queue topica){
        return BindingBuilder.bind(topica).to(topicExchange).with("topic.A");
    }

    @Bean
    public Binding bindingTopicAll(TopicExchange topicExchange, Queue topicall){
        return BindingBuilder.bind(topicall).to(topicExchange).with("topic.#");
    }

}
